package main

import (
	"fmt"
	"log"
	"net/http"
)

func main() {
	http.HandleFunc("/", indexHandler) // http的HandleFunc用于路由和Handler的绑定，这里设置两个路由/ & /hello，绑定indexHandler和helloHandler
	http.HandleFunc("/hello", helloHander)
	http.HandleFunc("/o", oHandler)
	http.HandleFunc("/p", func (w http.ResponseWriter, req *http.Request)  {
		fmt.Fprintf(w, "pp-URL.Path = %q\n", req.URL.Path)
	})
	log.Fatal(http.ListenAndServe(":9999", nil)) // ListenAndServe方法用于启动并监听一个端口
}

func indexHandler(w http.ResponseWriter, req *http.Request) {
	fmt.Fprintf(w, "URL.Path = %q\n", req.URL.Path)
}

func helloHander(w http.ResponseWriter, req *http.Request) {
	for k, v := range req.Header {
		fmt.Fprintf(w, "Header[%q] = %q\n", k, v)
	}
}

func oHandler(w http.ResponseWriter, req *http.Request) {
	fmt.Fprintf(w, "匿名 URL.Path = %q\n", req.URL.Path)
}
